package com.wzt.greenhousemobile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.wzt.greenhouse.model.ControlInfo;
import com.wzt.greenhouse.model.MonitorInfo;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class CourseAdapter extends BaseAdapter {
	private List<CourseBean> courseBeans;
	private Context context;
	private String hidecmdString="";
	public static Map<View, CourseBean> objMap = new HashMap<View,CourseBean>();
	public CourseAdapter(List<CourseBean> courseBeans, Context context) {
		super();
		this.courseBeans = courseBeans;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return courseBeans.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return courseBeans.get(position);
	}

	@Override
	public long getItemId(int position) { 
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		final CourseBean courseBean = courseBeans.get(position);
		if(courseBean.getConvertView() != null)
			return courseBean.getConvertView();
		LayoutInflater mInflater = (LayoutInflater) ((Activity) context)
				.getLayoutInflater();
		boolean changeImage = false;
		if (convertView == null) {
			if(courseBean.getType().equals("MonitorInfo"))
			{
				convertView = mInflater.inflate(R.layout.monitor_item, null);
				holder = new ViewHolder();
				holder.imageView = (ImageView) convertView.findViewById(R.id.image);
				holder.textView = (TextView) convertView.findViewById(R.id.text);
				holder.rl = (RelativeLayout) convertView.findViewById(R.id.rl);
				//Log.i("getView", "+++++:list" + position + ",width" + holder.rl.getLayoutParams().width);
				convertView.setTag(holder);
				TextView stateView = (TextView) convertView.findViewById(R.id.MState);
				MonitorInfo info = (MonitorInfo)courseBean.getObject();
				stateView.setText(info.getValue());
				
			}
			if(courseBean.getType().equals("ControlInfo"))
			{
				ControlInfo info = (ControlInfo)courseBean.getObject();
				info.getAnimation().setBackColor(courseBean.getBackgroundColor());
				switch (info.getType()) {
				case 1:
				case 2:
					convertView = mInflater.inflate(R.layout.control_3btn, null);
					holder = new ViewHolder();
					holder.imageView = (ImageView) convertView.findViewById(R.id.ctrl3_image);
					holder.textView = (TextView) convertView.findViewById(R.id.ctrl3_text);
					holder.rl = (RelativeLayout) convertView.findViewById(R.id.ctrl3_rl);
					convertView.setTag(holder);
					ImageView upView = (ImageView)convertView.findViewById(R.id.upBtn);
					ImageView downView = (ImageView)convertView.findViewById(R.id.downBtn);
					ImageView thirdView = (ImageView)convertView.findViewById(R.id.thirdBtn);
					info.setImageView(holder.imageView);
					info.bindImageViewByButton(upView, info.getUpButton());
					info.bindImageViewByButton(downView, info.getDownButton());
					info.bindImageViewByButton(thirdView, info.getOffButton());
					if(info.getType()==2)
					{
						upView.setBackgroundResource(R.drawable.open_off);
						downView.setBackgroundResource(R.drawable.close_off);
					}
					info.setBackgroundView(holder.imageView);
					switch (info.getType()) {
					case 1:
						if(info.getState().equals("upping"))
						{
							upView.setBackgroundResource(R.drawable.up_on);
							changeImage = true;
						}
						if(info.getState().equals("downing"))
						{
							downView.setBackgroundResource(R.drawable.down_on);
							changeImage  = true;
						}
						break;
					case 2:
						if(info.getState().equals("upping"))
						{
							upView.setBackgroundResource(R.drawable.open_on);
							changeImage = true;
						}
						if(info.getState().equals("downing"))
						{
							downView.setBackgroundResource(R.drawable.close_on);
							changeImage  = true;
						}
						
					default:
						break;
					}
					
					if(changeImage)
					{
						thirdView.setBackgroundResource(R.drawable.on);
						
						switch (info.getType()) {
						case 1:
							switch (info.getImageGroup()) {
							case 1:
								holder.imageView.setBackgroundResource(R.drawable.jm_1_5);
								break;
							case 2:
								holder.imageView.setBackgroundResource(R.drawable.jm_2_5);
								break;
							case 3:
								holder.imageView.setBackgroundResource(R.drawable.jm_3_5);
								break;
							case 4:
								holder.imageView.setBackgroundResource(R.drawable.jm_4_5);
								break;
							case 5:
								holder.imageView.setBackgroundResource(R.drawable.jm_5_5);
								break;
							case 6:
								holder.imageView.setBackgroundResource(R.drawable.jm_6_1);
								break;
							default:
								break;
							}
							break;
						case 2:
							holder.imageView.setBackgroundResource(R.drawable.jl_10);
							break;
						default:
							break;
						}						
					}
					break;
				case 3:
				case 4:
				case 5:
					convertView = mInflater.inflate(R.layout.control_item, null);
					holder = new ViewHolder();
					holder.imageView = (ImageView) convertView.findViewById(R.id.ctrl_image);
					holder.textView = (TextView) convertView.findViewById(R.id.ctrl_text);
					holder.rl = (RelativeLayout) convertView.findViewById(R.id.ctrl_rl);
					convertView.setTag(holder);
					ImageView onImageView = (ImageView) convertView.findViewById(R.id.onoffBtn);
					info.setImageView(holder.imageView);
					info.bindImageViewByButton(onImageView, info.getOnButton());
					info.setBackgroundView(holder.imageView);
					if(info.getState().equals("running"))
					{
						onImageView.setBackgroundResource(R.drawable.on);
						switch (info.getType()) {
						case 3:
							holder.imageView.setBackgroundResource(R.drawable.fs_1);
							break;
						case 4:
							holder.imageView.setBackgroundResource(R.drawable.dg_1);
							break;
						case 5:
							holder.imageView.setBackgroundResource(R.drawable.sb_1);
							break;
						default:
							break;
						}
						changeImage = true;
					}
					break;
				case 6:
					convertView = mInflater.inflate(R.layout.blank_item, null);
					holder = new ViewHolder();
					holder.rl = (RelativeLayout) convertView.findViewById(R.id.ctrl_blank_rl);
					convertView.setTag(holder);
					info.setImageView(holder.imageView);
					break;
				default:
					break;
				}
			}
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(courseBean.getImageView()!=0)
		{
			if(!changeImage)
			{
				holder.imageView.setBackgroundResource(courseBean.getImageView());
			}
			else {
				ControlInfo info = (ControlInfo)courseBean.getObject();
				info.resetAnimation();
			}
			holder.textView.setText(courseBean.getFunctionName());
		}
		holder.rl.setBackgroundResource(courseBean.getBackgroundColor());
		//holder.rl.setBackgroundResource(R.drawable.item_bg);
		convertView.setPadding(2, 2, 2, 2);
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Intent i = new Intent(context,VideoTypelistActivity.class);
				//context.startActivity(i);
				HardwareManager.setActiveTimer();
				CourseBean cBean = objMap.get(v);
				if(cBean.getType().equals("MonitorInfo"))
				{
					MonitorInfo info = (MonitorInfo)cBean.getObject();
					hidecmdString += info.getHideCmd();
					if(hidecmdString.indexOf("416231") >= 0)
					{
						hidecmdString="";
						exit();
					}
				}
			}

		});
		courseBean.setConvertView(convertView);
		objMap.put(convertView,courseBean);
		return convertView;
	}
	
	private void exit()
	{
		android.os.Process.killProcess(android.os.Process.myPid());		
	}

	public class ViewHolder {
		public ImageView imageView;
		public TextView textView;
		public RelativeLayout rl;
	}
}
